package com.penghaisoft.microwms.product.service.impl;

import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.ProCheckIn;
import com.penghaisoft.microwms.product.dao.MdProModelMapper;
import com.penghaisoft.microwms.product.dao.ProCheckInMapper;
import com.penghaisoft.microwms.product.service.IProCheckInService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务逻辑实现类
 * </p>
 *
 * @author
 * @createDate
 **/
@Service("proCheckInService")
public class ProCheckInServiceImpl extends BaseService implements IProCheckInService {
    @Resource
    private ProCheckInMapper proCheckInMapper;

    @Resource
    private MdProModelMapper mdProModelMapper;

    /**
     * 新增记录
     *
     * @param
     * @return
     */
    @Override
    public Resp create(ProCheckIn proCheckIn) {
        // 获得 品名、定量、规格、单位
        MdProModel mdProModel = new MdProModel();
        mdProModel.setModel(proCheckIn.getModel());
        List<MdProModel> models = mdProModelMapper.queryByAny(mdProModel);
        if (models.size()!=0){
            mdProModel = models.get(0);
            if(models.size() > 1){
            	return fail("本次选择的型号 " + proCheckIn.getModel() + " 在系统数据中有重复，请联系运维人员！");
            }
        }
        proCheckIn.setProductName(mdProModel.getProductName());
        proCheckIn.setDensity(mdProModel.getDensity());
        proCheckIn.setSpec(mdProModel.getSpec());
        proCheckIn.setUnit(mdProModel.getUnit());
        proCheckIn.setUserDefined10("A");
//        proCheckIn.setMachineNumber(mdProModel.getMachineNumber());//获取型号对应 生产机器号
        proCheckIn.setUserDefined2("0");

        proCheckInMapper.create(proCheckIn);
        return success();
    }

    /**
     * 删除记录（逻辑删除）
     *
     * @param
     * @return
     */
    @Override
    public Resp delete(ProCheckIn proCheckIn) {
        proCheckInMapper.delete(proCheckIn);
        return success();
    }

    /**
     * 查询列表
     *
     * @param
     * @param
     * @return
     */
    @Override
    public Pager<ProCheckIn> findListByCondition(int page, int rows, ProCheckIn condition) {
        Pager<ProCheckIn> pager = new Pager<>();
        pager.setPage(page);
        pager.setRows(rows);
        condition.preQuery();
        long size = proCheckInMapper.queryCount(condition);
        List<ProCheckIn> records = new ArrayList<ProCheckIn>();
        if (size > 0) {
            records = proCheckInMapper.queryList(pager, condition);
        }
        pager.setRecords(records);
        pager.setTotalCount(size);
        return pager;
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @Override
    public ProCheckIn findById(String id) {
        return proCheckInMapper.queryById(id);
    }

    /**
     * 修改记录
     *
     * @param
     * @return
     */
    @Override
    public Resp update(ProCheckIn proCheckIn) {
        proCheckInMapper.updateBySelect(proCheckIn);
        return success();
    }

}
